<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
     
    class processService{
        
        function connectData()
        {
            $mysql_host = "localhost";
            $mysql_database = "RegisterDB";
            $mysql_user = "root";
            $mysql_password = "";
            $con = mysql_connect($mysql_host, $mysql_user, $mysql_password );
            if (!$con)
            {
                die('Dont connect to Database: ' . mysql_error());
            }
            mysql_select_db($mysql_database, $con);
        }
    //===============================================================================

    //===============================================================================
     /*  XML structure
            <DataSet>
              <UserInfo>
                <Id>1</Id>
                <Name>Nhat</Name>
                <Email>nguyen89nhat@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
              <UserInfo>
                <Id>2</Id>
                <Name>Trang</Name>
                <Email>Trang66@gmail.com</Email>
                <Flag>0</Flag>
              </UserInfo>
            </DataSet>
      */
        
    //============================================================================================//
        function insertData($table,$xmlstring, $Method, $ClientBit)
        {
            if(!empty($xmlstring))
            {
//                $string = "";
//                $xml = new DOMDocument();
//                $xml->loadXML($xmlstring);
                
//                if($xml->schemaValidate("XmlSchema.xsd"))
//                {  
//                    $xdoc = $xml->documentElement;
//                    foreach ($xdoc->childNodes  as $item)
//                    {
//                        $string .= $item->nodeName . $item->nodeValue;
//                    }
//                    print $string;
                    $this->connectData();
                    $query = "insert into TableData(TableName,DataValue,Method,ClientBit) values ('".$table."', '".$xmlstring."', '".$Method."', '".$ClientBit."')";
                    mysql_query($query);
//                }
            }
        }
        //============================================================================================/
       
      //Lay du lieu do client yeu cau  
        function GetData($tablename, $Method, $ClientBit)
        {
           $queryselect = "";
           
           $queryupdate = "";
           
           $querydelete = "delete from TableData where ClientBit = '111'";
           
            $queryselect = "select DataValue from  TableData where TableName = '".$tablename."' and Method = '".$Method."' and ClientBit & '".$ClientBit."' = '000'";

            $queryupdate = "Update TableData set ClientBit = (ClientBit | '". $ClientBit. "') where TableName = '".$tablename."' and Method = '". $Method. "' and ClientBit & '".$ClientBit."' = '000'";

            $xmlstr = $this->selectData($queryselect);

            //Danh dau client nao da nhan du lieu
             if(!empty($xmlstr))
             {
                 $this->UpdateClientBit($queryupdate);
             }  
           //Delete nhung record da duoc tat ca cac Client truy xua
           $this->deleteData($querydelete);
           
           return $xmlstr;
        }
        
       //Lay du lieu tu bang 
        function  selectData($query)
        {
           $xmlstring = "";
           $this->connectData();
           $result = mysql_query($query);
           
           if(mysql_num_rows($result) > 0)
           {
               while($arr = mysql_fetch_row($result))
               {
                   foreach($arr as $key)
                       $xmlstring .= $key;
               }
           }
           if(!empty($xmlstring))
                $xmlstring = "<DataSet>".$xmlstring."</DataSet>";
           return $xmlstring;
        }
        
        //Chinh du data trong cot Bit***
        function UpdateClientBit($query)
        {
            $this->connectData();
            mysql_query($query);
        }
    //============================================================================================//
        function deleteData($query)
        {
             $this->connectData();
             mysql_query($query);
        }
    //============================================================================================//
    
    //Lay thoi gian hien tai cua he thong
        function getCurrentTime()
        {
            return date("Y-m-d H:i:s");
        }
        
    }
?>
